package com.example.boot.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mapper.AdminMapper;
import com.example.mapper.ProjectMapper;
import com.example.mapper.StageMapper;
import com.example.mapper.TeamMapper;
import com.example.pojo.entity.Admin;
import com.example.pojo.entity.Project;
import com.example.pojo.entity.Stage;
import com.example.pojo.entity.Team;
import com.example.pojo.utils.SendMessage;
import com.example.utils.SendMessageUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 阶段任务提醒
 * 阶段任务还有一个月结束提醒；阶段任务逾期提醒
 *
 * @author ding
 * @since 2024/7/29
 */
public class StageEmail extends QuartzJobBean {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private SendMessageUtils sendMessageUtils;

    @Autowired
    private StageMapper stageMapper;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<Project> projectList = projectMapper.selectList(new LambdaQueryWrapper<Project>()
                .eq(Project::getStatus, 3)
                .eq(Project::getIsDeleted, 0));

        for (Project project : projectList) {
            //获取每个项目的阶段信息
            List<Stage> stageList = stageMapper.selectList(new LambdaQueryWrapper<Stage>()
                    .eq(Stage::getProjectId, project.getId())
                    .eq(Stage::getIsDeleted, 0));

            //判断该阶段信息是否过期 或 是否还有一个月逾期
            for (Stage stage : stageList) {
                LocalDate end = stage.getEnd();
                if (LocalDate.now().plusMonths(1L).equals(end)){
                    Team team = teamMapper.selectById(project.getTeamId());
                    //获取队长信息
                    Admin captain = adminMapper.selectById(team.getCaptainId());

                    String content = "尊敬的" + team .getName() + "施工队队长" + captain.getName() + "您好，" + "您的施工队负责的项目" + project.getName()
                            + "的阶段" + stage.getName() + "还有一个月就到截止日期了，请确保阶段顺利进行，谢谢!!!";

                    SendMessage sendMessage = SendMessage.builder()
                            .to(captain.getEmail())
                            .content(content)
                            .subject("项目截止日期提醒")
                            .build();

                    sendMessageUtils.sendMessage(sendMessage);
                }else if (LocalDate.now().isAfter(end)){
                    Team team = teamMapper.selectById(project.getTeamId());
                    //获取队长信息
                    Admin captain = adminMapper.selectById(team.getCaptainId());

                    String content = "尊敬的" + team .getName() + "施工队队长" + captain.getName() + "您好，" + "您的施工队负责的项目" + project.getName()
                            + "的阶段" + stage.getName() + "已经逾期， 请尽快完成该阶段";

                    SendMessage sendMessage = SendMessage.builder()
                            .to(captain.getEmail())
                            .content(content)
                            .subject("项目截止日期提醒")
                            .build();

                    sendMessageUtils.sendMessage(sendMessage);
                }
            }
        }
    }
}
